package com.softeem.god.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.god.comm.R;
import com.softeem.god.entity.Album;
import com.softeem.god.entity.Music;
import com.softeem.god.service.AlbumService;
import com.softeem.god.service.MusicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 专辑管理:专辑列表、专辑详情、专辑添加、专辑修改、专辑删除
 */
@Tag(name = "专辑管理", description = "专辑管理接口")
@RestController
@RequestMapping("/album")
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    @Operation(summary = "专辑列表", description = "分页查询专辑列表")
    @Parameters(value = {
            @Parameter(name = "page", description = "当前页码"),
            @Parameter(name = "limit", description = "每页显示数量")
    })
    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer page, 
                  @RequestParam(defaultValue = "5") Integer limit) {
        // 分页查询select * from album limit ?,?
        IPage<Album> ipage = new Page<>(page, limit);
        // 执行查询select * from album limit ?,?
        List<Album> list = albumService.list(ipage);
        // 返回数据
        return R.success().setData(list).setCount(ipage.getTotal());
    }

    @Operation(summary = "所有专辑", description = "查询所有专辑列表")
    @GetMapping("/listAll")
    public R listAll() {
        return R.success().setData(albumService.list());
    }

    @Operation(summary = "专辑详情", description = "根据ID查询专辑详情")
    @Parameters(value = {
            @Parameter(name = "id", description = "专辑ID")
    })
    @GetMapping("/details")
    public R details(@RequestParam Integer id) {
        Album album = albumService.getById(id);
        if (album != null) {
            return R.success().setData(album);
        } else {
            return R.error().setMsg("专辑不存在");
        }
    }

    @Operation(summary = "添加专辑", description = "添加新专辑")
    @PostMapping("/add")
    public R add(@RequestBody Album album) {
        boolean result = albumService.save(album);
        if (result) {
            return R.success().setMsg("添加成功");
        } else {
            return R.error().setMsg("添加失败");
        }
    }

    @Operation(summary = "修改专辑", description = "根据ID修改专辑信息")
    @Parameters(value = {
            @Parameter(name = "id", description = "专辑ID")
    })
    @PutMapping("/update")
    public R update(@RequestBody Album album) {
        boolean result = albumService.updateById(album);
        if (result) {
            return R.success().setMsg("修改成功");
        } else {
            return R.error().setMsg("修改失败");
        }
    }

    @Autowired
    private MusicService musicService;
    
    @Operation(summary = "删除专辑", description = "根据ID删除专辑")
    @Parameters(value = {
            @Parameter(name = "id", description = "专辑ID")
    })
    @DeleteMapping("/delete")
    public R delete(@RequestParam Integer id) {
        // 检查是否有音乐关联此专辑
        QueryWrapper<Music> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid", id);
        
        List<Music> musicList = musicService.list(queryWrapper);
        if (musicList.size() > 0) {
            return R.error().setMsg("删除失败，该专辑下还有音乐文件");
        }
        
        boolean result = albumService.removeById(id);
        if (result) {
            return R.success().setMsg("删除成功");
        } else {
            return R.error().setMsg("删除失败");
        }
    }
}
